package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Author : wangjie
* @CreateDate : 2020/3/26 17:07
* @Version : 1.0
* @Description : 测试:用户前端控制器
*/
@RestController
@RequestMapping("/user")

public class UserController {
    //依赖注入
    @Reference
    private UserService userService;

    /**
    * @Author : wangjie
    * @CreateDate : 2020/4/15 14:10
    * @Version : 1.0
    * @Description : 条件查询,新增用户
    */
    @RequestMapping("delete")
    public Result delete(Integer id){
        try {
            userService.deleteById(id);
            return new Result(true,"删除用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除用户失败");
        }
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody User user){
        try {
            userService.edit(user);
            return new Result(true,"用户信息编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"用户信息编辑失败");
        }
    }

    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            User user=  userService.findById(id);
            return new Result(true,"用户数据回调成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"用户数据回调失败");
        }
    }

    @RequestMapping("add")
    public Result add(@RequestBody User user){
        try {
            userService.add(user);
            return new Result(true,"添加用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加用户失败");
        }
    }

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        PageResult pageResult=null;
        try {
            pageResult=userService.queryPage(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            pageResult=new PageResult(0l,null);
        }
        return pageResult;
    }

    /*动态获取显示菜单数据
    * 孙艺*/
    @RequestMapping("getMenuList")
    public Result getMenuList(@RequestParam("username") String username){
        try {
            /*调用service方法获取菜单数据*/
            List<Menu> mapsenuList = userService.getMenuList(username);
            return new Result(true, MessageConst.GET_MENU_SUCCESS, mapsenuList);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConst.GET_MENU_FAIL);
        }
    }


    /**
     * 获取用户登录名
     * @return
     */
    @RequestMapping("getUsername")
    public Result getUserName(){
        try {
            //从授权框架上下文获取认证对象
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            //从认证对象中获取用户对象
            org.springframework.security.core.userdetails.User user =(org.springframework.security.core.userdetails.User) authentication.getPrincipal();
            //根据用户 获取用户名
            String username = user.getUsername();
            //将用户名响应给客户端
            return new Result(true,MessageConst.ACTION_SUCCESS,username);
        } catch (Exception e) {
            e.printStackTrace();
            //响应失败的结果给客户端
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("login")
    public Result login(String username,String password){
        //打印前端用户登录信息
        System.out.println("oms_back:u:"+username+",p:"+password);
        //判断登录信息
        if (userService.login(username,password)){
            System.out.println("login success!");
            return new Result(true, MessageConst.ACTION_SUCCESS);
        }else {
            System.out.println("login fail!");
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 登录成功
     * @return
     */
    @RequestMapping("loginSuccess")
    public Result loginSuccess(){
        return new Result(true,MessageConst.LOGIN_SUCCESS);
    }

    /**
     * 登录失败
     * @return
     */
    @RequestMapping("loginFail")
    public Result loginFail(){
        return new Result(false,"登录失败");
    }
}
